package com.neusoft.neuiotms.dcom.converter.change;

import java.util.Date;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcom.dto.change.ChangePlanDTO;
import com.neusoft.neuiotms.dcom.model.change.ChangePlan;
import com.neusoft.neuiotms.dcom.model.change.ChangePlanAttachment;
import com.neusoft.neuiotms.dcom.repository.change.ChangePlanRepository;

@Component
public class ChangePlanConverter extends AbstractConverter<ChangePlan, ChangePlanDTO> {

    @Autowired
    private ChangePlanRepository changePlanRepository;

    @Autowired
    private ChangePlanAttachmentConverter changePlanAttachmentConverter;

    @Override
    public ChangePlan toModel(final ChangePlanDTO dto) {
        ChangePlan model;
        if (dto.isNew()) {
            model = new ChangePlan();
        } else {
            model = this.changePlanRepository.getOne(dto.getId());
        }
        model.setTaskId(dto.getTaskId());
        model.setWorkOrderNum(dto.getWorkOrderNum());
        model.setStartTime(dto.getStartTime());
        model.setEndTime(dto.getEndTime());
        model.setInterruptBusiness(dto.getInterruptBusiness());
        model.setSignificantChange(dto.getSignificantChange());
        model.setRisk(dto.getRisk());
        model.setRiskManagement(dto.getRiskManagement());
        model.setImplementPlan(dto.getImplementPlan());
        model.setFallbackPlan(dto.getFallbackPlan());
        model.setUpdateTime(new Date());

        if ((dto.getAttachments() != null) && !dto.getAttachments().isEmpty()) {
            final List<ChangePlanAttachment> list =
                    this.changePlanAttachmentConverter.toListModel(dto.getAttachments());
            list.forEach(t -> t.setChangePlan(model));
            model.setAttachments(list);
        }

        return model;
    }

    @Override
    public ChangePlanDTO toDTO(final ChangePlan model, final boolean forListView) {
        final ChangePlanDTO dto = new ChangePlanDTO();
        dto.setId(model.getId());
        dto.setTaskId(model.getTaskId());
        dto.setWorkOrderNum(model.getWorkOrderNum());
        dto.setStartTime(model.getStartTime());
        dto.setEndTime(model.getEndTime());
        dto.setInterruptBusiness(model.getInterruptBusiness());
        dto.setSignificantChange(model.getSignificantChange());
        dto.setRisk(model.getRisk());
        dto.setRiskManagement(model.getRiskManagement());
        dto.setImplementPlan(model.getImplementPlan());
        dto.setFallbackPlan(model.getFallbackPlan());
        dto.setCreateTime(model.getCreateTime());
        dto.setUpdateTime(model.getUpdateTime());

        if (!forListView) {
            if (CollectionUtils.isNotEmpty(model.getAttachments())) {
                dto.setAttachments(this.changePlanAttachmentConverter.toListDTO(model.getAttachments()));
            }
        }

        return dto;
    }
}
